/*
* Copyright (C) Microsoft. All rights reserved.  
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not 
* use this file except in compliance with the License.  You may obtain a copy 
* of the License at http://www.apache.org/licenses/LICENSE-2.0  
* 
* THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED 
* WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
* MERCHANTABLITY OR NON-INFRINGEMENT. 
*
* See the Apache License, Version 2.0 for specific language governing 
* permissions and limitations under the License.
*
*/
#pragma once

#include <baja/common.hpp>
#include <baja/math.hpp>

namespace baja
{

class linear_interpolation
{
public:
    linear_interpolation() {}

    linear_interpolation(
        float32 t0,
        float32 t1,
        float32 a,
        float32 b
        ) : _t0(t0), _t1(t1), _a(a), _b(b)
    {
        _v = (_b - _a) / (_t1 - _t0);
    }

    float32 apply(const float32 t)
    {
        if (t <= _t0)
        {
            return _a;
        }
        else if (t <= _t1)
        {
            return _v * (t - _t0) + _a;
        }
        else
        {
            return _b;
        }
    }

    float32 a() { return _a; }
    float32 b() { return _b; }
	float32 t0() { return _t0; }
	float32 t1() { return _t1; }

private:
    float32 _t0, _t1, _t2, _t3;
    float32 _a, _b;
    float32 _v;
};

template <class Tv>
class linear_interpolation_2d
{
public:
    linear_interpolation_2d() {}

    linear_interpolation_2d(
        float32 t0,
        float32 t3,
        Tv a,
        Tv b
        )
    {
        _tvix = linear_interpolation(t0, t3, a.x, b.x);
        _tviy = linear_interpolation(t0, t3, a.y, b.y);
    }

    Tv apply(const float32 t)
    {
        Tv res;
        res.x = _tvix.apply(t);
        res.y = _tviy.apply(t);
        return res;
    }

private:
    linear_interpolation _tvix;
    linear_interpolation _tviy;
};

template <class Tv>
class linear_interpolation_3d
{
public:
    linear_interpolation_3d() {}

    linear_interpolation_3d(
        float32 t0,
        float32 t3,
        Tv a,
        Tv b
		)
    {
        _tvix = linear_interpolation(t0, t3, a.x, b.x);
        _tviy = linear_interpolation(t0, t3, a.y, b.y);
        _tviz = linear_interpolation(t0, t3, a.z, b.z);
    }

    Tv apply(const float32 t)
    {
        Tv res;
        res.x = _tvix.apply(t);
        res.y = _tviy.apply(t);
        res.z = _tviz.apply(t);
        return res;
    }

private:
    linear_interpolation _tvix;
    linear_interpolation _tviy;
    linear_interpolation _tviz;
};

};